package com.mobi.Survey.lib.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.mobi.Survey.lib.model.Pojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * AbstractTable.java All CLasses of all tables extends this class.
 * 
 */
public abstract class AbstractTable {
	protected DBHelper dbHelper;
	public static final String ID = "_id";

	/**
 * 
 */
	public AbstractTable() {

	}

	/**
	 * Close the database connection which was opened
	 */
	public void closeDB() {
		if (this.dbHelper != null) {
			this.dbHelper.close();
			this.dbHelper = null;
			// printLogMessage("Database Connection Closed");
		}
	}

	/**
	 * Insert a new row in table with given of type of given Pojo
	 * 
	 * @param obj
	 * @return -1 if failed to insert new record
	 */
	public abstract long insertRow(Pojo obj);

	/**
	 * 
	 * @param obj
	 * @param surveyId
	 * @return -1 if failed to insert new record
	 */
	public long insertRow(Pojo obj, long surveyId) {
		return 0;
	}

	/**
	 * Delete all rows with given primary id in table
	 * 
	 * @param id
	 *            of row to delete
	 * @return true if deleted successfully
	 */
	public abstract boolean deleteRow(long id);

	/**
	 * Child classes calls this constructor as super(Context ctx) when they want
	 * to perform any database activity. Calling this construct will open new
	 * database connection
	 * 
	 * @param ctx
	 */
	public AbstractTable(Context ctx) {
		dbHelper = new DBHelper(ctx);
	}

	/**
	 * child class must implement this method and provide table creation SQL
	 */
	public abstract boolean createTable(SQLiteDatabase sqliteDb);

	/**
	 * Prints a message on console
	 * 
	 * @param message
	 */
	protected void printMessage(String message) {
		System.out.println(this.getClass().getName() + ": " + message);
	}

	/**
	 * Prints a message on Log
	 * 
	 * @param message
	 */
	protected void printLogMessage(String message) {
		Log.d(this.getClass().getName(), message);
	}

	public abstract void deleteAllData();

}
